package net.sf.bluecove.swing;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.bluecove.tester.log.Logger;
import org.bluecove.tester.util.RuntimeDetect;

import net.sf.bluecove.*;
import net.sf.bluecove.se.BlueCoveSpecific;
import net.sf.bluecove.se.FileStorage;
import net.sf.bluecove.se.JavaSECommon;

import org.bluecove.*;

public class ZLayout extends JFrame {

    /** This layout class is the default initialiser class that shows the layout
	 *  for my Bluetooth Swing program
	 */
	private static final long serialVersionUID = 1L;
	private JButton bs;
	private JButton bx;	
	private JButton cn;
	private JButton ls;
	private JLabel title;
    private JLabel enabled;
    private JLabel pressed;
    private JLabel armed;
	private Object menuBluetooth;
    
	public createAppearence extends JPanel implements ActionListener {
			public void windowClosing(WindowEvent w) {
				quit();}}
			
		
        //Sets Properties for buttons
        title = new JLabel("Bluetooth Device Discovery");
        bs = new JButton("Search for Bluetooth Devices");
        bx = new JButton("Stop Searching for Bluetooth Devices");
        cn = new JButton("Connect to device");
        ls = new JButton("List services");
        JCheckBox cb = new JCheckBox("Enabled", true);
        JTextArea ta = new JTextArea("Hello");
        
        //Set Appearance of items
        ta.setBorder(new EtchedBorder());
		     
        //Sets Location for the items in the area
		
        ta.setBounds(300, 35, 200, 200);
        title.setBounds(300, 10, 400, 20);
        ls.setBounds(500, 70, 220, 25);
        cn.setBounds(500, 40, 220, 25);
        bs.setBounds(10, 40, 220, 25);
        bx.setBounds(10, 80, 220, 25);
        cb.setBounds(200, 110, 80, 20);
        
        setTitle("Swingtooth Software");
        
        JPanel panel = new JPanel();
        panel.setLayout(null);
        
        enabled = new JLabel("Enabled: true");
        enabled.setBounds(40, 100, 90, 25);
        pressed = new JLabel("Pressed: false");
        pressed.setBounds(40, 130, 90, 25);
        armed  = new JLabel("Armed: false");
        armed.setBounds(40, 160, 90, 25);
        
        
        //Add items to the panel
        panel.add(title);
        panel.add(ls);
        panel.add(cn);
        panel.add(bs);
        panel.add(bx);
        panel.add(cb);
        panel.add(ta);
        panel.add(enabled);
        panel.add(pressed);
        panel.add(armed);

        add(panel);



      	setSize(750, 350);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

	
	
		
		JavaSECommon.initOnce();
		Configuration.storage = new FileStorage();
        
        //Sets commands for buttons
        bs.addChangeListener(new ChangeListener() {
        	
            public void stateChanged(ChangeEvent e) {

            DefaultButtonModel clientStart = (DefaultButtonModel) bs.getModel();
            if (clientStart.isEnabled())
            	//This is the action that happens when the search is commenced
                enabled.setText("Enabled: true");
            else	
                enabled.setText("Enabled: false");
            if (clientStart.isArmed())
            	//when button is depressed
                armed.setText("Armed: true");
            else
                armed.setText("Armed: false");

            if (clientStart.isPressed())
				try {
					Switcher.startClient();
					bs.setEnabled(false);
					bx.setEnabled(true);
					//System.out.println(Switcher.client);
					//Switcher.client.printLocalDeviceInfo();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					System.exit(ERROR);
				}
            	pressed.setText("Pressed: true");
            	

            }

        }); 
        
        bx.addActionListener(new ActionListener()	{
        	
        	public void actionPerformed(ActionEvent e){
        	DefaultButtonModel clientStop = (DefaultButtonModel) bs.getModel();
            if (clientStop.isPressed())
				try {
					bs.setEnabled(true);
					bx.setEnabled(false);
					Switcher.clientShutdown();
					//System.out.println(Switcher.client);
					//Switcher.client.printLocalDeviceInfo();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					System.exit(ERROR);
				}
        	}
        }); 
        cb.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (bs.isEnabled())
                    bs.setEnabled(false);
                else
                    bs.setEnabled(true);
            }
        });
       
    

	
    public static void main(String[] args) {
		Logger.debug("Stated app");
		Logger.debug("OS:" + System.getProperty("os.name") + "|" + System.getProperty("os.version") + "|"
				+ System.getProperty("os.arch"));
		Logger.debug("Java:" + System.getProperty("java.vendor") + " " + System.getProperty("java.version") + " " +System.getProperty("java.vm.version"));
		//Logger.addAppender(this);
		//BlueCoveSpecific.addAppender(this);
		new createAppearence();
		new Layout();

    }
	private void quit() {
		Logger.debug("quit");
		Switcher.clientShutdown();
		Switcher.serverShutdownOnExit();

		Rectangle b = this.getBounds();
		Configuration.storeData("main.x", String.valueOf(b.x));
		Configuration.storeData("main.y", String.valueOf(b.y));
		Configuration.storeData("main.height", String.valueOf(b.height));
		Configuration.storeData("main.width", String.valueOf(b.width));

		//Logger.removeAppender(this);
		BlueCoveSpecific.removeAppender();

		// this.dispose();
		System.exit(0);
	}
}
